package com.example

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import kotlin.math.atan2
import kotlin.math.cos
import kotlin.math.min
import kotlin.math.sin

class CircularDialView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {

    private val paint = Paint().apply {
        isAntiAlias = true
        strokeWidth = 4f
        color = Color.BLUE
        style = Paint.Style.STROKE
    }

    private val tickPaint = Paint().apply {
        isAntiAlias = true
        color = Color.BLUE
        strokeWidth = 2f
    }

    private val pointerPaint = Paint().apply {
        isAntiAlias = true
        color = Color.MAGENTA
        style = Paint.Style.FILL
    }

    private var sweepAngle = 180f
    private var startAngle = 180f
    private var rotationAngle = 0.0

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)

        val radius = min(width, height) / 2f
        val centerX = width / 2f
        val centerY = height / 2f

        // Save the canvas state
        canvas.save()
        // Rotate the canvas based on the rotationAngle
        canvas.rotate(rotationAngle.toFloat(), centerX, centerY)

        // Draw the circular arc
        canvas.drawArc(
            centerX - radius,
            centerY - radius,
            centerX + radius,
            centerY + radius,
            startAngle,
            sweepAngle,
            false,
            paint
        )

        // Draw tick marks
        val tickCount = 20
        val tickAngleStep = sweepAngle / tickCount
        for (i in 0..tickCount) {
            val angle = Math.toRadians((startAngle + i * tickAngleStep).toDouble())
            val startX = centerX + radius * cos(angle).toFloat()
            val startY = centerY + radius * sin(angle).toFloat()
            val stopX = centerX + (radius - 20) * cos(angle).toFloat()
            val stopY = centerY + (radius - 20) * sin(angle).toFloat()
            canvas.drawLine(startX, startY, stopX, stopY, tickPaint)
        }

        // Restore the canvas state to its original position
        canvas.restore()

        // Draw the fixed pointer
        val pointerAngle = Math.toRadians((startAngle + sweepAngle / 2).toDouble())
        val pointerX = centerX + (radius - 40) * cos(pointerAngle).toFloat()
        val pointerY = centerY + (radius - 40) * sin(pointerAngle).toFloat()
        canvas.drawCircle(pointerX, pointerY, 20f, pointerPaint)
    }

    override fun onTouchEvent(event: MotionEvent): Boolean {
        when (event.action) {
            MotionEvent.ACTION_DOWN, MotionEvent.ACTION_MOVE -> {
                val centerX = width / 2f
                val centerY = height / 2f
                val touchX = event.x
                val touchY = event.y

                // Calculate the angle of the touch point
                val angle = atan2(touchY - centerY, touchX - centerX).toDouble()
                rotationAngle = Math.toDegrees(angle)
                invalidate()
                return true
            }
        }
        return super.onTouchEvent(event)
    }
}
